#include "Config.h"

namespace hps {

_Config::_Config() {
    loadDataBase();
    m_iconSize = QSize(25, 25);
    m_tabWidgetIconSize = QSize(21, 21);
    m_editorFirstWordFont.setFamily("Arial");
    m_editorFirstWordFont.setPointSize(noteBookCfg.EditorTextFontSize);   // 设置字体大小
    m_editorFirstWordFont.setBold(true);      // 设置加粗
    m_editorFirstWordPalette.setColor(QPalette::WindowText, QColor("#6e6e6e"));
    m_editorTextEditFont.setPointSize(noteBookCfg.EditorTextFontSize);
}
_Config::~_Config() {
    outPutJsonCfg();
}
void _Config::loadDataBase() {
    try {
        std::ifstream file("/home/hps/hps/Markdown-cpp/DataBase/noteBooks.json");
        std::string jsonString;
        if (file . is_open()) {
            std::string line;
            while (std::getline(file, line)) {
                jsonString += line;
            }
            file . close();
        } else {
            std::cerr << "Unable to open !" << std::endl;
        }

        j = boost::json::parse(jsonString).as_object();
        noteBookCfg.DataBaseName = boost::json::value_to<std::string>(j. at("DataBaseName"));
        boost::json::array noteBookList = j. at("NoteBookList").as_array();

        for (const auto& p : noteBookList) {
            boost::json::object _noteBookObj = p.as_object();
            for (const auto& pair : _noteBookObj) {
                noteBookCfg.NoteBookMap[pair.key().data()] = pair.value().as_string().data();
            }
        }

        noteBookCfg.CreateTime = boost::json::value_to<std::string>(j. at("CreateTime"));
        noteBookCfg.UpdateTime = boost::json::value_to<std::string>(j. at("UpdateTime"));
        noteBookCfg.Description = boost::json::value_to<std::string>(j. at("Description"));
        noteBookCfg.EditorTextFontSize = boost::json::value_to<int>(j.at("EditorTextFontSize"));
        file.close();
    } catch (std::exception& e) {
        std::cerr << e.what() << std::endl;
    }
}
void _Config::setEditorTextPointSize(int size) {
    m_editorTextEditFont.setPointSize(size);
    j["EditorTextFontSize"] = size;
    noteBookCfg.EditorTextFontSize = size;
}
void _Config::reLoadDataBase() {
    loadDataBase();
}
void _Config::addNoteBook(const std::string &bookName, std::string &bookPath) {
    noteBookCfg.NoteBookMap[bookName] = bookPath;
    boost::json::object noteBook;
    noteBook[bookName] = bookPath;
    j["NoteBookList"].as_array().push_back(noteBook);
    outPutJsonCfg();
    reLoadDataBase();
}
void _Config::outPutJsonCfg() {
    std::string configStr = boost::json::serialize(j);
    // 指定要写入的文件路径
    std::string filePath = std::string(_noteBookCfgPath);

    // 打开文件流，准备写入数据
    std::ofstream outFile(filePath);

    if (outFile.is_open()) {
        // 将 JSON 字符串写入文件
        outFile << configStr;
        // 关闭文件流
        outFile.close();
    } else {
        std::cerr << "update json cfg error! " << __FILE__ << ":" << __LINE__ << std::endl;
    }
}
void _Config::updateDataBaseTime() {
    j["UpdateTime"] = hps::TimeFormat(hps::GetCurrentMS());
}
}